//
//  GuesModel.m
//  ChatForHot
//
//  Created by FLY on 2018/10/18.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "GuestModel.h"
#import "DBUtility.h"
#import "ChatDemoHelper.h"

@implementation GuestModel

+ (NSArray *)HOTCHATgetTopThree{
    
    __block NSMutableArray *resultAr = [[NSMutableArray alloc] init];
    DBUtility *dbutility = (DBUtility *)[DBUtility sharedbUtility];
    [dbutility open];
    [dbutility.fmdbQueue inDatabase:^(FMDatabase *db) {
        
        @try {
            
            NSString *sql =@"SELECT headPic from guestlist where myid = :myid and issee != 1 order by date desc limit 0,3";
            
            NSDictionary *para = [[NSDictionary alloc] initWithObjectsAndKeys:HOTCHATAPPINFO.accountid,@"myid", nil];
            
            FMResultSet *results = [db executeQuery:sql withParameterDictionary:para];
            
            while( [results next] ){
                
                [resultAr addObject:[results stringForColumn:@"headPic"]];
            }
            
            [results close];
        }
        @catch (NSException *exception) {
            NSLog(@"%@",NSStringFromClass([self class]));
        }
        
    }];
    [dbutility close];
    return resultAr;
}
+ (NSArray *)HOTCHATgetAllList{
    
    __block NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    DBUtility *dbutility = (DBUtility *)[DBUtility sharedbUtility];
    [dbutility open];
    [dbutility.fmdbQueue inDatabase:^(FMDatabase *db) {
        
        @try {
            
            NSString *sql = @"SELECT * from guestlist where myid = :myid order by date desc limit 0,20";
            
            NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:HOTCHATAPPINFO.accountid,@"myid", nil];
            
            FMResultSet *results = [db executeQuery:sql withParameterDictionary:dic];
            
            while( [results next] ){
                
                GuestModel *model = [[GuestModel alloc] init];
                
                model.accountid = [results stringForColumn:@"accountid"];
                model.nickName = [results stringForColumn:@"nickName"];
                model.headPic = [results stringForColumn:@"headPic"];
                model.date = [results stringForColumn:@"date"];
                model.other = [results stringForColumn:@"other"];
                model.vip = [results stringForColumn:@"vip"];
                model.IMUser = [results stringForColumn:@"IMUser"];
                
                [resultArr addObject:model];
            }
            
            [results close];
        }
        @catch (NSException *exception) {
            NSLog(@"%@",NSStringFromClass([self class]));
        }
        
    }];
    [dbutility close];
    return resultArr;
}
+ (void)HOTCHATremarkAllissee{
    
    DBUtility *dbutility = [DBUtility sharedbUtility];
    [dbutility open];
    [dbutility.fmdbQueue inDeferredTransaction:^(FMDatabase *db, BOOL *rollback) {
        NSString *sql = @"update guestlist set issee = 1 where myid = :myid";
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:HOTCHATAPPINFO.accountid,@"myid", nil];
        
        if ( ![db executeUpdate:sql withParameterDictionary:dic] ) {
//            DB_ERROR_LOG(NSStringFromClass([self class]))
            *rollback = YES;
            return;
        }
    }];
    [dbutility close];
}
+ (NSDictionary *)HOTCHATsaveGuestArray:(NSArray *)guestArr{
    
    __block NSDictionary *resultDic = nil;
    DBUtility *dbutility = [DBUtility sharedbUtility];
    [dbutility open];
    [dbutility.fmdbQueue inDeferredTransaction:^(FMDatabase *db, BOOL *rollback) {
        NSString *sql;
        
        for (EMMessage *message in guestArr) {
            // cmd消息中的扩展属性
            NSDictionary *ext = message.ext;
            
            if ([message.body isKindOfClass:[EMCmdMessageBody class]]) {
                EMCmdMessageBody *body = (EMCmdMessageBody *)message.body;
                if (![body.action isEqualToString:@"guest"]) {
                    continue;
                }
            }else{
                continue;
            }
            
            NSString *accountid = [ext valueForKey:@"accountid"];
            NSString *imSend = message.from;
            //            if (accountid.length == 0) {
            //                continue;
            //            }
            
            sql = @"delete from GuestList where myid = :myid and accountid = :accountid ";
            
            NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:HOTCHATAPPINFO.accountid,@"myid",accountid,@"accountid", nil];
            
            if ( ![db executeUpdate:sql withParameterDictionary:dic] ) {
//                DB_ERROR_LOG(NSStringFromClass([self class]))
                *rollback = YES;
                return;
            }
            
            sql = @"insert into guestlist(accountid,myid,IMUser,nickName,headPic,date,other,vip) values(:accountid,:myid,:IMUser,:nickName,:headPic,:date,:other,:vip)";
            
            NSMutableDictionary *dic2 = [[NSMutableDictionary alloc] init];
            [dic2 setValue:HOTCHATnilSpace([ext valueForKey:@"accountid"]) forKey:@"accountid"];
            [dic2 setValue:HOTCHATnilSpace([ext valueForKey:@"nickName"]) forKey:@"nickName"];
            [dic2 setValue:HOTCHATnilSpace([ext valueForKey:@"headPic"]) forKey:@"headPic"];
            [dic2 setValue:HOTCHATnilSpace([ext valueForKey:@"vip"]) forKey:@"vip"];
            [dic2 setValue:[NSNumber numberWithLongLong:message.localTime] forKey:@"date"];
            [dic2 setValue:HOTCHATnilSpace([ext valueForKey:@"other"]) forKey:@"other"];
            [dic2 setValue:HOTCHATnilSpace(HOTCHATAPPINFO.accountid) forKey:@"myid"];
            [dic2 setValue:HOTCHATnilSpace(imSend) forKey:@"IMUser"];
            
            if ( ![db executeUpdate:sql withParameterDictionary:dic2] ) {
//                DB_ERROR_LOG(NSStringFromClass([self class]))
                *rollback = YES;
                return;
            }
            resultDic = dic2;
        }
        
    }];
    [dbutility close];
    return resultDic;
}
NSString *HOTCHATnilSpace(NSString *vStr){
    if (vStr == nil) {
        return @"";
    }
    return vStr;
}
@end
